﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MessageProtocol
{
    public class Header : ISerializable
    {
        public uint MSGID { get; set; }
        public uint MSGTYPE { get; set; }
        public uint BODYLENGTH { get; set; }
        public byte LASTMSG { get; set; }

        public Header(){ }

        public Header(byte[] bytes)
        {
            MSGID = BitConverter.ToUInt32(bytes,0);
            MSGTYPE = BitConverter.ToUInt32(bytes, 4);
            BODYLENGTH = BitConverter.ToUInt32(bytes, 8);
            LASTMSG = bytes[12];
        }

        public byte[] GetBytes()
        {
            byte[] bytes = new byte[16];

            byte[] temp = BitConverter.GetBytes(MSGID);
            Array.Copy(temp, 0, bytes, 0, temp.Length);

            temp = BitConverter.GetBytes(MSGTYPE);
            Array.Copy(temp, 0, bytes, 4, temp.Length);

            temp = BitConverter.GetBytes(BODYLENGTH);
            Array.Copy(temp, 0, bytes, 8, temp.Length);

            bytes[12] = LASTMSG;

            return bytes;
        }

        public int GetSize()
        {
            return 16 ;
        }


        public object GetObject(byte[] bytes)
        {
            MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length);
            Header header = new Header();
            
            header.MSGID = BitConverter.ToUInt32(bytes, 0);
            header.MSGTYPE = BitConverter.ToUInt32(bytes, 4);
            header.BODYLENGTH = BitConverter.ToUInt32(bytes, 8);
            header.LASTMSG = bytes[12];
            
            return header;
            throw new NotImplementedException();
        }
    }
}
